package com.kjc.demo.basic.service;

import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.AmqpException;
import org.springframework.amqp.core.*;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.io.Serializable;

/**
 * @author ：keep
 * @version ：
 * @date ：Created in 18:43 2019/5/21
 * @description：
 * @modified by：
 */
@Slf4j
@Service
public class RabbitService {
    @Resource
    private AmqpTemplate template;

    public void send(String queue, Serializable object){
        template.convertAndSend(queue, object, new MessagePostProcessor() {
            @Override
            public Message postProcessMessage(Message message) throws AmqpException {
                message.getMessageProperties().setDeliveryMode(MessageDeliveryMode.PERSISTENT);
                return message;
            }
        });
    }

    public void sendDelayed(String exchange, String routeKey, Serializable object, long delayTimeMs){
        template.convertAndSend(exchange, routeKey, object, new MessagePostProcessor() {
            @Override
            public Message postProcessMessage(Message message) throws AmqpException {
                message.getMessageProperties().setDeliveryMode(MessageDeliveryMode.PERSISTENT);
                message.getMessageProperties().setHeader("x-delay", delayTimeMs);
                return message;
            }
        });
    }
    
    public void send1() {
    	String context = "hi, i am message 1";
    	System.out.println("Sender : " + context);
    	this.template.convertAndSend("exchange", "topic.message", context);
    }

    public void send2() {
    	String context = "hi, i am messages 2";
    	System.out.println("Sender : " + context);
    	this.template.convertAndSend("exchange", "topic.messages", context);
    }
}
